package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("/list")
   // @RequiredPermission(name = "部门页面", expression = "department:list")
    @RequiresPermissions(value ={"department:list","部门页面"},logical = Logical.OR )
    public String list(Model model, QueryObject qo) {
        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "/department/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    //@RequiredPermission(name = "部门删除", expression = "department:delete")
    @RequiresPermissions(value ={"department:delete","部门删除"},logical = Logical.OR )
    public JsonResult delete(Long id) {
        try {
            departmentService.delete(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "操作失败");
        }
    }

    @RequestMapping("/input")
    //@RequiredPermission(name = "部门编辑", expression = "department:input")
    @RequiresPermissions(value ={"department:input","部门编辑"},logical = Logical.OR )
    public String input(Model model, Long id) {
        if (id != null) {
            Department department = departmentService.selectOne(id);
            model.addAttribute("department", department);
        }
        return "/department/input";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    //@RequiredPermission(name = "部门保存或新增", expression = "department:saveOrUpdate")
    @RequiresPermissions(value ={"department:saveOrUpdate","部门保存或新增"},logical = Logical.OR )
    public JsonResult saveOrUpdate(Department department) {
        try {
            Long id = department.getId();
            if (id != null) {
                departmentService.update(department);
            } else {
                departmentService.insert(department);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, e.getMessage());
        }
    }
}
